﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LCL;
using LCL.Config;
using LCL.RedisCache.Config;
using ServiceStack.Redis;

namespace LCL.RedisCache
{
    /// <summary>
    ///  MemCache管理操作类
    /// </summary>
    public class RedisManager
    {
        /// <summary>
        /// redis配置文件信息
        /// </summary>
        private static RedisConfigInfo redisConfigInfo = RedisConfigs.GetConfig();
        private static PooledRedisClientManager prcm;
        /// <summary>
        /// 静态构造方法，初始化链接池管理对象
        /// </summary>
        static RedisManager()
        {
            CreateManager();
        }
        /// <summary>
        /// 创建链接池管理对象
        /// </summary>
        private static void CreateManager()
        {
            string[] writeServerList = Utils.SplitString(redisConfigInfo.WriteServerList, ",");
            string[] readServerList = Utils.SplitString(redisConfigInfo.ReadServerList, ",");

            prcm = new PooledRedisClientManager(readServerList, writeServerList,
                             new RedisClientManagerConfig
                             {
                                 MaxWritePoolSize = redisConfigInfo.MaxWritePoolSize,
                                 MaxReadPoolSize = redisConfigInfo.MaxReadPoolSize,
                                 AutoStart = redisConfigInfo.AutoStart,
                             });
        }
        /// <summary>
        /// 客户端缓存操作对象
        /// </summary>
        public static RedisClient GetClient()
        {
            if (prcm == null)
                CreateManager();
            return (RedisClient)prcm.GetClient();
        }
    }
}
